package com.ruoyi.safe.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Strings;
import com.ruoyi.common.core.domain.entity.CommonCheckType;
import com.ruoyi.common.core.domain.entity.ListType;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.*;
import com.ruoyi.safe.constant.AccidentHiddenConstants;
import com.ruoyi.safe.constant.DictTypeConstants;
import com.ruoyi.safe.domain.*;
import com.ruoyi.safe.domain.dto.BizProjectCheckRecordDTO;
import com.ruoyi.safe.domain.dto.BizProjectCheckRecordSearchDTO;
import com.ruoyi.safe.domain.dto.CheckRecordWebDTO;
import com.ruoyi.safe.domain.dto.RecordRegionDTO;
import com.ruoyi.safe.domain.vo.*;
import com.ruoyi.safe.mapper.*;
import com.ruoyi.safe.service.IBizExcludePlanTaskService;
import com.ruoyi.safe.service.IBizProjectCheckRecordService;
import com.ruoyi.safe.service.IBizSafeAttachService;
import com.ruoyi.safe.service.IBizSafeBillnoService;
import com.ruoyi.system.mapper.SysUserMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 项目自检-检查记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-20
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class BizProjectCheckRecordServiceImpl implements IBizProjectCheckRecordService {

    private final BizProjectCheckRecordMapper bizProjectCheckRecordMapper;

    private final BizDutyRegionMapper bizDutyRegionMapper;

    private final BizSafeAttachMapper bizSafeAttachMapper;

    private final BizExcludePlanTaskMapper bizExcludePlanTaskMapper;

    private final IBizSafeAttachService bizSafeAttachService;

    private final BizRectifyReviewMapper bizRectifyReviewMapper;

    private final SysUserMapper sysUserMapper;

    private final ListTypeMapper listTypeMapper;

    private final BizExtensionRequestsMapper bizExtensionRequestsMapper;

    private final BizSafeFineMapper bizSafeFineMapper;

    private final IBizExcludePlanTaskService bizExcludePlanTaskService;

    private final CommonCheckTypeMapper commonCheckTypeMapper;

    private final ListConstructionMapper listConstructionMapper;

    private final ListDeviseMapper listDeviseMapper;

    private final ListMaintenanceMapper listMaintenanceMapper;

    private final ListManageMapper listManageMapper;

    private final CommonCooperatorMapper commonCooperatorMapper;

    private final IBizSafeBillnoService bizSafeBillnoService;

    private final CommonProjectMapper commonProjectMapper;

    /**
     * 查询项目自检-检查记录
     *
     * @param id 项目自检-检查记录主键
     * @return 项目自检-检查记录
     */
    @Override
    public BizProjectCheckRecord selectBizProjectCheckRecordById(Long id) {
        List<SysUser> sysUsers = sysUserMapper.selectUserListAll();
        BizProjectCheckRecord bizProjectCheckRecord = bizProjectCheckRecordMapper.selectBizProjectCheckRecordById(id);
        bizProjectCheckRecord.setReviewUserNickName(sysUsers.stream().filter(s -> bizProjectCheckRecord.getReviewUserName().equals(s.getUserName())).collect(Collectors.toList()).get(0).getNickName());
        bizProjectCheckRecord.setCheckPeopleNickName(sysUsers.stream().filter(s -> bizProjectCheckRecord.getCheckPeople().equals(s.getUserName())).collect(Collectors.toList()).get(0).getNickName());
        bizProjectCheckRecord.setRevisePeopleNickName(sysUsers.stream().filter(s -> bizProjectCheckRecord.getRevisePeople().equals(s.getUserName())).collect(Collectors.toList()).get(0).getNickName());
        if (!Strings.isNullOrEmpty(bizProjectCheckRecord.getNotifier())) {
            String[] split = bizProjectCheckRecord.getNotifier().split(",");
            ArrayList<String> strings = ListUtil.toList(split);
            Set<String> collect = sysUsers.stream().filter(s -> strings.contains(s.getUserName())).map(s -> s.getNickName()).collect(Collectors.toSet());
            String notifierNickName = StringUtils.join(collect, ",");
            bizProjectCheckRecord.setNotifierNickName(notifierNickName);
        }
        //项目名称
        CommonProject commonProject = commonProjectMapper.selectCommonProjectById(bizProjectCheckRecord.getProjectId());
        if(commonProject != null){
            bizProjectCheckRecord.setProjectName(commonProject.getName());
        }
        //检查类型
        CommonCheckType commonCheckType = commonCheckTypeMapper.selectListTypeByTypeNo(Long.valueOf(bizProjectCheckRecord.getCheckType()));
        if(commonCheckType != null){
            bizProjectCheckRecord.setCheckTypeName(commonCheckType.getCheckTypeName());
        }
        //事故隐患
        if(AccidentHiddenConstants.NORMAL.equals(bizProjectCheckRecord.getAccidentHidden())){
            bizProjectCheckRecord.setAccidentHiddenName(AccidentHiddenConstants.NORMAL_NAME);
        }else{
            bizProjectCheckRecord.setAccidentHiddenName(AccidentHiddenConstants.IMPORTANT_NAME);
        }
        //隐患等级
        String riskLevelName = DictUtils.getDictLabel(DictTypeConstants.LIST_RISK_LEVEL,bizProjectCheckRecord.getRiskLevel());
        if(riskLevelName != null){
            bizProjectCheckRecord.setRiskLevelName(riskLevelName);
        }
        //分包单位
        if(StringUtils.isNotEmpty(bizProjectCheckRecord.getSubpackageUnit())){
            List<Long> subpackageUnitIds = Arrays.stream(bizProjectCheckRecord.getSubpackageUnit().split(",")).map(item->Long.valueOf(item)).collect(Collectors.toList());
            List<CommonCooperator> commonCooperators = commonCooperatorMapper.selectSubPackageList();
            Map<Long,CommonCooperator> commonCooperatorMap = new HashMap<>();
            if(subpackageUnitIds.size() > 0){
                commonCooperatorMap = commonCooperators.stream().collect(Collectors.toMap(CommonCooperator::getId, Function.identity()));
            }
            StringBuffer subpackageUnitNameBuffer = new StringBuffer();
            for (int i = 0; i < subpackageUnitIds.size(); i++) {
                CommonCooperator commonCooperator = commonCooperatorMap.get(subpackageUnitIds.get(i));
                if(commonCooperator != null){
                    subpackageUnitNameBuffer.append(",");
                    subpackageUnitNameBuffer.append(commonCooperator.getEnterpriseName());
                }
            }
            if(subpackageUnitNameBuffer.length() > 0) {
                bizProjectCheckRecord.setSubpackageUnitNames(subpackageUnitNameBuffer.substring(1));
            }
        }
        List<BizSafeAttach> bizSafeAttaches = bizSafeAttachMapper.selectBizSafeAttachList(BizSafeAttach.builder().bizId(id).build());
        bizProjectCheckRecord.setAttachList(bizSafeAttaches);
        return bizProjectCheckRecord;
    }

    @Override
    public BizProjectCheckRecord selectBizRecordById(Long id) {
        return bizProjectCheckRecordMapper.selectBizProjectCheckRecordById(id);
    }

    /**
     * 查询项目自检-检查记录列表
     *
     * @param bizProjectCheckRecord 项目自检-检查记录
     * @return 项目自检-检查记录
     */
    @Override
    public List<BizProjectCheckRecord> selectBizProjectCheckRecordList(BizProjectCheckRecord bizProjectCheckRecord) {
        return bizProjectCheckRecordMapper.selectBizProjectCheckRecordList(bizProjectCheckRecord);
    }

    /**
     * 新增项目自检-检查记录
     *
     * @param bizProjectCheckRecord 项目自检-检查记录
     * @return 结果
     */
    @Override
    @Transactional
    public int insertBizProjectCheckRecord(BizProjectCheckRecord bizProjectCheckRecord) {
        BizProjectCheckRecordDTO bizProjectCheckRecordDTO = new BizProjectCheckRecordDTO();
        BeanUtil.copyProperties(bizProjectCheckRecord, bizProjectCheckRecordDTO);

        bizProjectCheckRecordDTO.setUserName(SecurityUtils.getUsername());
        this.handle(bizProjectCheckRecordDTO);
        return 1;
    }

    @Override
    @Transactional
    public int insertBizProjectCheckRecordNoLogin( BizProjectCheckRecordDTO bizProjectCheckRecordDTO) {
        this.handle(bizProjectCheckRecordDTO);
        return 1;
    }

    /**
     * 修改项目自检-检查记录
     *
     * @param bizProjectCheckRecord 项目自检-检查记录
     * @return 结果
     */
    @Override
    public int updateBizProjectCheckRecord(BizProjectCheckRecord bizProjectCheckRecord) {
        bizProjectCheckRecord.setUpdateTime(DateUtils.getNowDate());
        if (StringUtils.isNull(bizProjectCheckRecord.getUpdateBy())) {
            bizProjectCheckRecord.setUpdateBy(SecurityUtils.getUsername());
        }
        return bizProjectCheckRecordMapper.updateBizProjectCheckRecord(bizProjectCheckRecord);
    }

    /**
     * 批量删除项目自检-检查记录
     *
     * @param ids 需要删除的项目自检-检查记录主键
     * @return 结果
     */
    @Override
    public int deleteBizProjectCheckRecordByIds(Long[] ids) {
        return bizProjectCheckRecordMapper.deleteBizProjectCheckRecordByIds(ids);
    }

    /**
     * 删除项目自检-检查记录信息
     *
     * @param id 项目自检-检查记录主键
     * @return 结果
     */
    @Override
    public int deleteBizProjectCheckRecordById(Long id) {
        return bizProjectCheckRecordMapper.deleteBizProjectCheckRecordById(id);
    }

    /**
     * 查询项目自检下的所有数据
     */
    @Override
    public List<BizProjectCheckRecord> selectCheckRecordListById(Long id) {
        return bizProjectCheckRecordMapper.selectListByProjectCheckId(id);
    }

    @Override
    @Transactional
    public Boolean handle(BizProjectCheckRecordDTO bizProjectCheckRecordDTO) {

        //判断是否是未施工
        if ("5".equals(bizProjectCheckRecordDTO.getHiddenDangerStatus())) {
            //如果是未施工，调用getBizProjectCheckRecords方法，批量生成数据
            List<BizProjectCheckRecord> bizProjectCheckRecords = getBizProjectCheckRecords(bizProjectCheckRecordDTO);
            List<Long> planTaskIds = bizProjectCheckRecordDTO
                    .getList().stream().map(RecordRegionDTO::getId)
                    .collect(Collectors.toList());
            //批量修改任务状态
            bizExcludePlanTaskMapper.updateByPlanTaskIds(planTaskIds, "1");
            bizProjectCheckRecordMapper.insertBizProjectCheckRecordList(bizProjectCheckRecords);
            return true;
        }
        BizProjectCheckRecord bizProjectCheckRecord = new BizProjectCheckRecord();
        BeanUtil.copyProperties(bizProjectCheckRecordDTO, bizProjectCheckRecord);
        bizProjectCheckRecord.setSubpackageUnit(bizProjectCheckRecordDTO.getSubpackageUnitIds());
        //主键id,获取方式为雪花算法
        long snowflakeNextId = IdUtil.getSnowflakeNextId();
        if ("1".equals(bizProjectCheckRecordDTO.getHiddenDangerStatus())) {

            if (StringUtils.isEmpty(bizProjectCheckRecordDTO.getReviewUserName())) {
                BizDutyRegion bizDutyRegion = bizDutyRegionMapper.selectBizDutyRegionById(Long.parseLong(bizProjectCheckRecord.getDutyArea()));
                if (StringUtils.isNull(bizDutyRegion)) {
                    throw new ServiceException("责任区域不存在");
                }
                SysUser sysUser = sysUserMapper.selectUserById(bizDutyRegion.getReviewerUserId());
                bizProjectCheckRecord.setReviewUserName(sysUser.getUserName());
            }
        }
        if ("1".equals(bizProjectCheckRecordDTO.getIsFine())) {
            BizSafeFine bizSafeFine = new BizSafeFine();
            bizSafeFine.setRecordId(snowflakeNextId);
            bizSafeFine.setFineMoney(bizProjectCheckRecordDTO.getFineMoney());
            bizSafeFine.setFineExplain(bizProjectCheckRecordDTO.getFineExplain());
            bizSafeFine.setFineUser(bizProjectCheckRecordDTO.getSubpackageUnitIds());
            bizSafeFine.setFineUnit(sysUserMapper.selectUserByUserName(bizProjectCheckRecordDTO.getUserName()).getDept().getDeptName());
            bizSafeFine.setFineSource("1");
            bizSafeFine.setSafeNumber(bizSafeBillnoService.getSafeNumber(1L));
            bizSafeFine.setProjectId(bizProjectCheckRecordDTO.getProjectId());
            bizSafeFine.setFineButton("1");
            bizSafeFine.setCreateBy(bizProjectCheckRecordDTO.getUserName());
            bizSafeFine.setCreateTime(DateUtils.getNowDate());
            bizSafeFineMapper.insertBizSafeFine(bizSafeFine);
        }
        bizProjectCheckRecord.setCheckPeople(bizProjectCheckRecordDTO.getUserName());
        bizProjectCheckRecord.setId(snowflakeNextId);
        if (StringUtils.isEmpty(bizProjectCheckRecordDTO.getHiddenClass())) {
            getTypeName(bizProjectCheckRecord, bizProjectCheckRecordDTO.getTypeNo());
        }
        bizProjectCheckRecord.setCheckTime(DateUtils.getNowDate());
        bizProjectCheckRecord.setCreateTime(DateUtils.getNowDate());
        bizProjectCheckRecord.setCreateBy(bizProjectCheckRecordDTO.getUserName());
        bizProjectCheckRecord.setUpdateTime(DateUtils.getNowDate());

        bizProjectCheckRecordMapper.insertBizProjectCheckRecord(bizProjectCheckRecord);
        bizSafeAttachService.selectBizSafeAttachByNames(bizProjectCheckRecordDTO.getFileName(), snowflakeNextId);
        if (StringUtils.isNotNull(bizProjectCheckRecordDTO.getProjectCheckId())) {
            BizExcludePlanTask bizExcludePlanTask = bizExcludePlanTaskMapper.selectBizExcludePlanTaskById(bizProjectCheckRecordDTO.getProjectCheckId());
            //状态未0是可删除数据，1暂定不可删除数据
            bizExcludePlanTask.setStatus("1");
            bizExcludePlanTaskMapper.updateBizExcludePlanTask(bizExcludePlanTask);
        }
        return true;
    }

    private static List<BizProjectCheckRecord> getBizProjectCheckRecords(BizProjectCheckRecordDTO bizProjectCheckRecordDTO) {
        List<BizProjectCheckRecord> bizProjectCheckRecords = new ArrayList<>();
        bizProjectCheckRecordDTO.getList().forEach(s -> {
            BizProjectCheckRecord bizProjectCheckRecord = new BizProjectCheckRecord();
            BeanUtil.copyProperties(bizProjectCheckRecordDTO, bizProjectCheckRecord);
            bizProjectCheckRecord.setId(IdUtil.getSnowflakeNextId());
            bizProjectCheckRecord.setDutyArea(s.getRegionId());

            if (StringUtils.isNotNull(s.getId())) {
                bizProjectCheckRecord.setProjectCheckId(s.getId());

            }
            bizProjectCheckRecord.setCheckTime(DateUtils.getNowDate());
            bizProjectCheckRecord.setCheckType(s.getDateTypeValue());
            bizProjectCheckRecord.setCreateBy(bizProjectCheckRecordDTO.getUserName());
            bizProjectCheckRecord.setTypeNo(s.getTypeNo());
            bizProjectCheckRecord.setListId(s.getListId());
            bizProjectCheckRecord.setCheckPeople(bizProjectCheckRecordDTO.getUserName());
            bizProjectCheckRecords.add(bizProjectCheckRecord);
        });
        return bizProjectCheckRecords;
    }

    @Override
    public PageInfo<BizProjectCheckRecord> getRectify(ListRequestVo listRequestVo) {

        List<BizProjectCheckRecord> rectify = bizProjectCheckRecordMapper.getRectify(listRequestVo);

        List<BizDutyRegion> bizDutyRegions = bizDutyRegionMapper.selectBizDutyRegionList(BizDutyRegion.builder().projectId(listRequestVo.getProjectId()).build());

        rectify.forEach(bizProjectCheckRecord -> {

            ListType listType = listTypeMapper.selectListTypeById(Long.valueOf(bizProjectCheckRecord.getTypeNo()));

            String s1 = listType.getAncestors().split(",")[0];

            if ("1".equals(s1)) {
                ListDevise listDevise = listDeviseMapper.selectListDeviseById(bizProjectCheckRecord.getListId());
                bizProjectCheckRecord.setRiskFactor(listDevise.getRiskFactor());
            } else if ("2".equals(s1)) {
                ListConstruction listConstruction = listConstructionMapper.selectListConstructionById(bizProjectCheckRecord.getListId());
                bizProjectCheckRecord.setRiskFactor(listConstruction.getRiskFactor());
            } else if ("3".equals(s1)) {
                ListMaintenance listMaintenance = listMaintenanceMapper.selectListMaintenanceById(bizProjectCheckRecord.getListId());
                bizProjectCheckRecord.setRiskFactor(listMaintenance.getRiskFactor());
            } else {
                ListManage listManage = listManageMapper.selectListManageById(bizProjectCheckRecord.getListId());
                bizProjectCheckRecord.setRiskFactor(listManage.getManageBehaviors());
            }
            //如果是通知 且isRevise为0 不可整改状态
            getPeopleType(listRequestVo, bizProjectCheckRecord);
            Set<Long> parentsRegion = bizExcludePlanTaskService.getParentsRegion(bizDutyRegions, bizDutyRegions.stream()
                    .filter(s -> s.getId().toString().equals(bizProjectCheckRecord.getDutyArea()))
                    .findFirst()
                    .orElse(new BizDutyRegion()));

            bizProjectCheckRecord.setDutyArea(bizDutyRegions
                    .stream()
                    .filter(s -> parentsRegion.contains(s.getId()))
                    .collect(Collectors.toList())
                    .stream()
                    .map(BizDutyRegion::getRegionName)
                    .collect(Collectors.joining("/")));
            List<BizSafeAttach> bizSafeAttaches = bizSafeAttachMapper.selectBizSafeAttachList(BizSafeAttach.builder().bizId(bizProjectCheckRecord.getId()).build());
            bizProjectCheckRecord.setAttachList(bizSafeAttaches);
        });

        NameUtils.handleUserNameToNickName(rectify, sysUserMapper.selectUserListAll(), "checkPeople", "revisePeople", "reviewUserName", "notifier");
        return new PageInfo<>(rectify);
    }

    @Override
    public int selectBizProjectCheckRecordCount(ListRequestVo listRequestVo) {
        return bizProjectCheckRecordMapper.selectBizProjectCheckRecordCount(listRequestVo);
    }

    @Override
    public PageInfo<BizProjectCheckRecord> getReview(ListRequestVo listRequestVo) {

        //获取复查记录
        List<BizProjectCheckRecord> review = bizProjectCheckRecordMapper.getReview(listRequestVo);

        //遍历复查记录
        handleRectifyReview(review, sysUserMapper.selectUserListAll(), bizDutyRegionMapper.selectBizDutyRegionList(new BizDutyRegion()));
        return new PageInfo<>(review);
    }

    @Override
    public List<BizProjectCheckRecord> selectBizProjectCheckRecord(Long projectId, Long planId, String hiddenDangerStatus) {
        List<String> hiddenDangerStatusList = new ArrayList<>();
        if (StringUtils.isNotEmpty(hiddenDangerStatus)) {
            hiddenDangerStatusList = Arrays.asList(hiddenDangerStatus.split(","));
        }

        List<BizExcludePlanTask> bizExcludePlanTasks = bizExcludePlanTaskMapper.selectBizExcludePlanTaskListByPlanId(planId);
        List<Long> taskIds = bizExcludePlanTasks.stream().map(BizExcludePlanTask::getId).collect(Collectors.toList());
        if (taskIds.isEmpty()) {
            return new ArrayList<>();
        }
        List<BizProjectCheckRecord> bizProjectCheckRecords = bizProjectCheckRecordMapper.selectBizProjectCheckRecordByTaskIds(projectId, taskIds, hiddenDangerStatusList);
        handleRectifyReview(bizProjectCheckRecords, sysUserMapper.selectUserListAll(), bizDutyRegionMapper.selectBizDutyRegionList(new BizDutyRegion()));
        NameUtils.handleUserNameToNickName(bizProjectCheckRecords, sysUserMapper.selectUserListAll(), "checkPeople", "revisePeople", "reviewUserName", "notifier");
        return bizProjectCheckRecords;
    }

    @Override
    public BizProjectCheckRecordVO getRectifyByRecordId(ListRequestVo listRequestVo) {
        if (listRequestVo.getRecordId() == null || listRequestVo.getProjectId() == null) {
            throw new ServiceException("参数错误");
        }
        List<SysUser> sysUsers = sysUserMapper.selectUserListAll();

        BizProjectCheckRecord bizProjectCheckRecord = bizProjectCheckRecordMapper.selectBizProjectCheckRecordByIdAndProjectId(listRequestVo);
        if (StrUtil.isNotEmpty(bizProjectCheckRecord.getCheckPeople())) {
            bizProjectCheckRecord.setCheckPeoplePhone(sysUsers.stream()
                    .filter(s -> bizProjectCheckRecord.getCheckPeople().equals(s.getUserName()))
                    .findFirst().orElse(new SysUser())
                    .getPhonenumber());
        }

        if (StrUtil.isNotEmpty(bizProjectCheckRecord.getReviewUserName())) {
            bizProjectCheckRecord.setReviewUserPhone(sysUsers.stream()
                    .filter(s -> bizProjectCheckRecord.getReviewUserName().equals(s.getUserName()))
                    .findFirst().orElse(new SysUser())
                    .getPhonenumber());
        }

        if (StrUtil.isNotEmpty(bizProjectCheckRecord.getRevisePeople())) {
            bizProjectCheckRecord.setRevisePeoplePhone(sysUsers.stream()
                    .filter(s -> bizProjectCheckRecord.getRevisePeople().equals(s.getUserName()))
                    .findFirst().orElse(new SysUser())
                    .getPhonenumber());
        }

        if (StrUtil.isNotEmpty(bizProjectCheckRecord.getHiddenDangerStatus())) {
            DictUtils.getDictLabel("biz_check_result", bizProjectCheckRecord.getHiddenDangerStatus());
        }
        List<BizProjectCheckRecord> bizProjectCheckRecords = Collections.singletonList(bizProjectCheckRecord);
        handleRectifyReview(bizProjectCheckRecords, sysUsers, bizDutyRegionMapper.selectBizDutyRegionList(BizDutyRegion.builder().projectId(listRequestVo.getProjectId()).build()));
        BizProjectCheckRecordVO bizProjectCheckRecordVO = new BizProjectCheckRecordVO();
        getPeopleType(listRequestVo, bizProjectCheckRecord);

//        NameUtils.handleUserNameToNickName(bizProjectCheckRecords, sysUsers, "checkPeople", "revisePeople", "reviewUserName", "notifier");

        BeanUtil.copyProperties(bizProjectCheckRecord, bizProjectCheckRecordVO);
        // 检查人nickname
        List<SysUser> collect = sysUsers.stream().filter(u -> u.getUserName().equals(bizProjectCheckRecordVO.getCheckPeople())).collect(Collectors.toList());
        if (collect.size() > 0) {
            bizProjectCheckRecordVO.setCheckPeopleNickName(collect.get(0).getNickName());
        }
        // 整改人nickName
        collect = sysUsers.stream().filter(u -> u.getUserName().equals(bizProjectCheckRecordVO.getRevisePeople())).collect(Collectors.toList());
        if (collect.size() > 0) {
            bizProjectCheckRecordVO.setRevisePeopleNickName(collect.get(0).getNickName());
        }
        // 复查人nickName
        collect = sysUsers.stream().filter(u -> u.getUserName().equals(bizProjectCheckRecordVO.getReviewUserName())).collect(Collectors.toList());
        if (collect.size() > 0) {
            bizProjectCheckRecordVO.setReviewUserNameNickName(collect.get(0).getNickName());
        }
        // 通知人nickName
        if (StrUtil.isNotEmpty(bizProjectCheckRecord.getNotifier())) {
            bizProjectCheckRecordVO.setNotifier(bizProjectCheckRecord.getNotifier().split(","));
            String[] stringStream = sysUsers.stream().filter(u -> ArrayUtil.contains(bizProjectCheckRecordVO.getNotifier(), u.getUserName())).map(u -> u.getNickName()).toArray(String[]::new);
            bizProjectCheckRecordVO.setNotifierName(stringStream);
        }
        bizProjectCheckRecordVO.setRecordId(bizProjectCheckRecord.getId().toString());
        return bizProjectCheckRecordVO;
    }

    private void getPeopleType(ListRequestVo listRequestVo, BizProjectCheckRecord bizProjectCheckRecord) {
        List<String> peopleType = new ArrayList<>();
        if (StrUtil.isNotEmpty(bizProjectCheckRecord.getNotifier()) && Arrays.asList(bizProjectCheckRecord.getNotifier().split(",")).contains(listRequestVo.getUserName())) {
            peopleType.add("0");
        }
        if (ObjectUtil.equals(listRequestVo.getUserName(), bizProjectCheckRecord.getRevisePeople())) {
            peopleType.add("1");
        }
        if (ObjectUtil.equals(listRequestVo.getUserName(), bizProjectCheckRecord.getReviewUserName())) {
            peopleType.add("2");
        }
        if (ObjectUtil.equals(listRequestVo.getUserName(), bizProjectCheckRecord.getCheckPeople())) {
            peopleType.add("3");
        }
        bizProjectCheckRecord.setPeopleType(StrUtil.join(",", peopleType));
    }

    @Override
    public int updateProjectCheckRecord(BizProjectCheckRecordDTO bizProjectCheckRecordDTO) {
        BizProjectCheckRecord bizProjectCheckRecord = new BizProjectCheckRecord();
        BeanUtil.copyProperties(bizProjectCheckRecordDTO, bizProjectCheckRecord);
        bizProjectCheckRecord.setUpdateTime(DateUtils.getNowDate());
        bizProjectCheckRecord.setUpdateBy(bizProjectCheckRecordDTO.getUserName());
        return bizProjectCheckRecordMapper.updateBizProjectCheckRecord(bizProjectCheckRecord);

    }

    @Override
    public int getReformCount(ListRequestVo listRequestVo) {
        return bizProjectCheckRecordMapper.getReformCount(listRequestVo);

    }

    @Override
    public List<String> selectCheckPeopleList() {
        return bizProjectCheckRecordMapper.selectCheckPeopleList();
    }

    @Override
    public List<BizProjectCheckRecord> selectAllRecordList(String createTime) {
        return bizProjectCheckRecordMapper.selectAllRecordList(createTime);
    }

    @Override
    public StatisticsAndDataVO selectCheckLedger(BizProjectCheckRecordSearchDTO searchDTO) {

        if (StringUtils.isNull(searchDTO.getProjectId())) {
            return new StatisticsAndDataVO();
        }
        if (StringUtils.isNotEmpty(searchDTO.getLoginName())) {
            searchDTO.setLoginName(SecurityUtils.getUsername());
        }

        if (StringUtils.isNotNull(searchDTO.getDateRange())) {

            Date date = searchDTO.getDateRange()[1];

            // Convert to LocalDateTime
            LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();

            // Set the time to 23:59:59
            localDateTime = localDateTime.with(LocalTime.of(23, 59, 59));

            // Convert back to Date
            date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());

            searchDTO.setDateRange(new Date[]{searchDTO.getDateRange()[0], date});
        }

        //获取复查记录(带分页)
        List<BizProjectCheckRecordVO2> bizProjectCheckRecordVO2S = bizProjectCheckRecordMapper.selectCheckLedger(searchDTO);

        //获取复查记录(不带分页)
        List<BizProjectCheckRecordVO2> bizProjectCheckRecordVO2List = bizProjectCheckRecordMapper.selectCheckLedger(searchDTO);

        //获取责任区域
        List<BizDutyRegion> bizDutyRegions = bizDutyRegionMapper.selectBizDutyRegionList(BizDutyRegion.builder().projectId(searchDTO.getProjectId()).build());

        //获取分包单位
        List<CommonCooperator> commonCooperators = commonCooperatorMapper.selectCommonCooperatorList(new CommonCooperator());

        //查询出所有用户
        List<SysUser> sysUsers = sysUserMapper.selectUserListAll();

        StatisticsAndDataVO statisticsAndDataVO = new StatisticsAndDataVO();
        statisticsAndDataVO.setList(bizProjectCheckRecordVO2S);


        bizProjectCheckRecordVO2S.forEach(item -> {

            BizDutyRegion bizDutyRegion = bizDutyRegions.stream()
                    .filter(s -> s.getId().toString().equals(item.getDutyArea()))
                    .findFirst()
                    .orElse(new BizDutyRegion());

            Set<Long> parentsRegion = bizExcludePlanTaskService.getParentsRegion(bizDutyRegions, bizDutyRegion);

            String regionNames = getRegionNames(bizDutyRegions, parentsRegion);

            //查询是否有延期记录
            List<BizExtensionRequests> bizExtensionRequests = bizExtensionRequestsMapper.selectBizExtensionRequestsList(BizExtensionRequests.builder().recordId(item.getId()).build());

            String isApply = bizExtensionRequests.stream().anyMatch(s -> "0".equals(s.getStatus())) || item.getOverdueRectification().equals("1") ? "0" : "1";
            item.setIsApply(isApply);
            item.setDutyArea(regionNames);

            if (StrUtil.isNotEmpty(item.getSubpackageUnit())) {

                BizProjectCheckRecord record = new BizProjectCheckRecord();

                record.setSubpackageUnit(item.getSubpackageUnit());
                record.setId(item.getId());

                handleSubpackageUnit(sysUsers, record, commonCooperators);

                item.setSubpackageUnit(record.getSubpackageUnit());

                item.setBizSafeFineVOList(record.getBizSafeFineVOList());
            }

            CommonProject commonProject = commonProjectMapper.selectCommonProjectById(Long.parseLong(item.getProjectName()));

            item.setProjectName(commonProject.getName());

            if (StrUtil.isNotEmpty(item.getRiskLevel())) {
                item.setRiskLevel(DictUtils.getDictLabel("list_risk_level", item.getRiskLevel()));
            }
        });

        NameUtils.handleUserNameToNickName(bizProjectCheckRecordVO2S, sysUserMapper.selectUserListAll(), "checkPeople", "revisePeople", "reviewUserName");

        //合格数据
        long closedNum = bizProjectCheckRecordVO2List.stream().filter(s -> "3".equals(s.getHiddenDangerStatus())).count();
        statisticsAndDataVO.setClosedNum(closedNum);

        //不合格(待整改，待复查)
        long unclosedNum = bizProjectCheckRecordVO2List.stream().filter(s -> Arrays.asList("1", "2").contains(s.getHiddenDangerStatus())).count();
        statisticsAndDataVO.setUnclosedNum(unclosedNum);

        //待整改
        long toBeRectifiedNum = bizProjectCheckRecordVO2List.stream().filter(s -> "1".equals(s.getHiddenDangerStatus())).count();
        statisticsAndDataVO.setToBeRectifiedNum(toBeRectifiedNum);

        //待复查
        long toBeReviewedNum = bizProjectCheckRecordVO2List.stream().filter(s -> "2".equals(s.getHiddenDangerStatus())).count();
        statisticsAndDataVO.setToBeReviewedNum(toBeReviewedNum);

        //超期未闭合
        long overdueUnclosedNum = bizProjectCheckRecordVO2List.stream().filter(s -> Arrays.asList("1", "2").contains(s.getHiddenDangerStatus()) && "1".equals(s.getOverdue())).count();
        statisticsAndDataVO.setOverdueUnclosedNum(overdueUnclosedNum);

        //整改率=(合格+待复查)/(合格+待复查+待整改)
        BigDecimal rectificationRate = closedNum + toBeReviewedNum + toBeRectifiedNum == 0 ? BigDecimal.ZERO :
                BigDecimal.valueOf(closedNum + toBeReviewedNum).divide(BigDecimal.valueOf(closedNum + toBeReviewedNum + toBeRectifiedNum), 2, RoundingMode.HALF_UP)
                        .multiply(BigDecimal.valueOf(100));
        statisticsAndDataVO.setRectificationRate(rectificationRate);

        //闭合率=合格/(合格+待复查+待整改)
        BigDecimal closedRate = closedNum + toBeReviewedNum + toBeRectifiedNum == 0 ? BigDecimal.ZERO :
                BigDecimal.valueOf(closedNum).divide(BigDecimal.valueOf(closedNum + toBeReviewedNum + toBeRectifiedNum), 2, RoundingMode.HALF_UP)
                        .multiply(BigDecimal.valueOf(100));
        statisticsAndDataVO.setClosedRate(closedRate);

        return statisticsAndDataVO;

    }

    @Override
    public BizProjectCheckRecord selectBizProjectCheckRecordVO2ById(Long id, Long projectCheckId) {
        List<BizProjectCheckRecord> bizProjectCheckRecords = null;
        BizProjectCheckRecord bizProjectCheckRecord = new BizProjectCheckRecord();
        List<SysUser> sysUsers = sysUserMapper.selectUserListAll();
        if (ObjectUtil.isNotNull(id)) {
            bizProjectCheckRecord = bizProjectCheckRecordMapper.selectBizProjectCheckRecordById(id);
        }else  if (ObjectUtil.isNotNull(projectCheckId)) {
            bizProjectCheckRecords = bizProjectCheckRecordMapper.selectListByProjectCheckId(projectCheckId);
            if(bizProjectCheckRecords != null && bizProjectCheckRecords.size() > 0){
                bizProjectCheckRecord = bizProjectCheckRecords.get(0);
            }
        }
        bizProjectCheckRecords = Collections.singletonList(bizProjectCheckRecord);
        handleRectifyReview(bizProjectCheckRecords, sysUsers, bizDutyRegionMapper.selectBizDutyRegionList(new BizDutyRegion()));
        NameUtils.handleUserNameToNickName(bizProjectCheckRecords, sysUsers, "checkPeople", "revisePeople", "reviewUserName", "notifier");

        return bizProjectCheckRecords.get(0);

    }

    @Override
    public Boolean updateNoticePeopleById(BizProjectCheckRecordDTO bizProjectCheckRecordDTO) {

        BizProjectCheckRecord bizProjectCheckRecord = new BizProjectCheckRecord();
        bizProjectCheckRecord.setId(bizProjectCheckRecordDTO.getRecordId());

        // 获取现有记录
        BizProjectCheckRecord existingRecord = bizProjectCheckRecordMapper.selectBizProjectCheckRecordById(bizProjectCheckRecordDTO.getRecordId());

        // 分割现有通知者
        Set<String> existingNotifiers = Arrays.stream(existingRecord.getNotifier().split(",")).filter(StringUtils::isNotEmpty).collect(Collectors.toSet());

        // 添加新的通知者
        Set<String> notifiers = Arrays.stream(bizProjectCheckRecordDTO.getNotifier().split(",")).collect(Collectors.toSet());

        existingNotifiers.addAll(notifiers);

        // 使用distinct删除重复项，并使用","连接通知者
        String updatedNotifiers = existingNotifiers.stream().distinct().collect(Collectors.joining(","));

        // 将更新后的通知者设置给记录
        existingRecord.setNotifier(updatedNotifiers);

        // 在数据库中更新记录
        bizProjectCheckRecordMapper.updateBizProjectCheckRecord(existingRecord);

        return true;
    }

    /**
     * 处理整改复查
     */
    private void handleRectifyReview(List<BizProjectCheckRecord> review, List<SysUser> sysUsers, List<BizDutyRegion> regionList) {

        List<CommonCooperator> commonCooperators = commonCooperatorMapper.selectCommonCooperatorList(new CommonCooperator());

        for (BizProjectCheckRecord bizProjectCheckRecord : review) {

            bizProjectCheckRecord.setCheckPeopleValue(bizProjectCheckRecord.getCheckPeople());

            if (StrUtil.isNotEmpty(bizProjectCheckRecord.getRiskLevel())) {
                bizProjectCheckRecord.setRiskLevel(DictUtils.getDictLabel("list_risk_level", bizProjectCheckRecord.getRiskLevel()));
            }

            //获取整改记录
            handleRectifyReviews(bizProjectCheckRecord, sysUsers);

            //查看是否有延期记录
            handleExtensionRequests(bizProjectCheckRecord);

            //区域名称
            handleDutyArea(regionList, bizProjectCheckRecord);

            //添加图片信息
            List<BizSafeAttach> bizSafeAttaches = bizSafeAttachMapper.selectBizSafeAttachList(BizSafeAttach.builder().bizId(bizProjectCheckRecord.getId()).build());
            bizProjectCheckRecord.setAttachList(bizSafeAttaches);

            //分包单位和罚款逻辑处理
            if (StrUtil.isNotEmpty(bizProjectCheckRecord.getSubpackageUnit())) {
                handleSubpackageUnit(sysUsers, bizProjectCheckRecord, commonCooperators);
            }
            //检查类型
            bizProjectCheckRecord.setCheckType(commonCheckTypeMapper.selectListTypeByTypeNo(Long.valueOf(bizProjectCheckRecord.getCheckType())).getCheckTypeName());
        }
    }

    /**
     * 未添加的检查记录数据
     */
    @Override
    public List<BizProjectCheckRecord> selectUnRecordList(BizProjectCheckRecord projectCheckRecord) {
        return bizProjectCheckRecordMapper.selectUnRecordList(projectCheckRecord);
    }


    @Override
    public String getRegionNames(List<BizDutyRegion> bizDutyRegions, Set<Long> parentsRegion) {

        return bizDutyRegions
                .stream()
                .filter(s -> parentsRegion.contains(s.getId()))
                .collect(Collectors.toList())
                .stream().sorted(Comparator.comparing(BizDutyRegion::getId))
                .map(BizDutyRegion::getRegionName)
                .collect(Collectors.joining("/"));
    }

    @Override
    public List<BizProjectCheckRecord> selectListByProjectCheckId(Long projectCheckId) {
        List<BizProjectCheckRecord> bizProjectCheckRecords =  bizProjectCheckRecordMapper.selectListByProjectCheckId(projectCheckId);
        return bizProjectCheckRecords;
    }

    @Override
    @Transactional
    public int addCheckRecord(CheckRecordWebDTO checkRecordWebDTO) {
        BizProjectCheckRecord bizProjectCheckRecord = new BizProjectCheckRecord();
        BeanUtil.copyProperties(checkRecordWebDTO, bizProjectCheckRecord);
        long snowflakeNextId = IdUtil.getSnowflakeNextId();
        bizProjectCheckRecord.setId(snowflakeNextId);
        bizProjectCheckRecord.setCreateBy(SecurityUtils.getUsername());
        bizProjectCheckRecord.setCreateTime(DateUtils.getNowDate());
        if (StrUtil.isNotEmpty(checkRecordWebDTO.getTypeNo()) && ObjectUtil.isNotNull(checkRecordWebDTO.getListId())) {
            getTypeName(bizProjectCheckRecord, checkRecordWebDTO.getTypeNo());
        }
        bizProjectCheckRecord.setUpdateBy(SecurityUtils.getUsername());
        bizProjectCheckRecord.setUpdateTime(DateUtils.getNowDate());
        bizProjectCheckRecord.setCheckPeople(SecurityUtils.getUsername());
        bizProjectCheckRecord.setCheckTime(DateUtils.getNowDate());
        bizProjectCheckRecordMapper.insertBizProjectCheckRecord(bizProjectCheckRecord);
        bizSafeAttachService.selectBizSafeAttachByNames(checkRecordWebDTO.getCheckPhoto(), snowflakeNextId);
        if (ObjectUtil.equals("3", checkRecordWebDTO.getHiddenDangerStatus())) {
            //整改记录
            BizRectifyReview bizRectifyReview = new BizRectifyReview();
            long snowflakeNextId1 = IdUtil.getSnowflakeNextId();
            bizRectifyReview.setId(snowflakeNextId1);
            bizRectifyReview.setOperatorer(checkRecordWebDTO.getRevisePeople());
            bizRectifyReview.setOperatorStatus("1");
            bizRectifyReview.setOperatorTime(checkRecordWebDTO.getReviseTime());
            bizRectifyReview.setOperatorType("2");
            bizRectifyReview.setOperatorExplain(checkRecordWebDTO.getReviseReply());
            bizRectifyReview.setRecordId(snowflakeNextId);
            bizRectifyReview.setCreateBy(SecurityUtils.getUsername());
            bizRectifyReview.setCreateTime(DateUtils.getNowDate());
            bizRectifyReviewMapper.insertBizRectifyReview(bizRectifyReview);
            bizSafeAttachService.selectBizSafeAttachByNames(checkRecordWebDTO.getRevisePhoto(), snowflakeNextId1);
            //复查记录
            bizRectifyReview = new BizRectifyReview();
            long snowflakeNextId2 = IdUtil.getSnowflakeNextId();
            bizRectifyReview.setId(snowflakeNextId2);
            bizRectifyReview.setOperatorer(checkRecordWebDTO.getReviewUserName());
            bizRectifyReview.setOperatorStatus("3");
            bizRectifyReview.setOperatorTime(checkRecordWebDTO.getReviewTime());
            bizRectifyReview.setOperatorType("1");
            bizRectifyReview.setOperatorExplain(checkRecordWebDTO.getReviewReply());
            bizRectifyReview.setRecordId(snowflakeNextId);
            bizRectifyReview.setCreateBy(SecurityUtils.getUsername());
            bizRectifyReview.setCreateTime(DateUtils.getNowDate());
            bizRectifyReviewMapper.insertBizRectifyReview(bizRectifyReview);
            bizSafeAttachService.selectBizSafeAttachByNames(checkRecordWebDTO.getReviewPhoto(), snowflakeNextId2);
        }
        return 1;
    }

    private void getTypeName(BizProjectCheckRecord bizProjectCheckRecord, String typeNo) {
        List<ListType> listTypes = listTypeMapper.selectListTypeList(new ListType());
        String[] split = listTypes.stream()
                .filter(s -> s.getId().toString().equals(typeNo))
                .findFirst()
                .orElse(new ListType()).getAncestors().split(",");

        bizProjectCheckRecord.setHiddenClass(listTypes.stream()
                .filter(s -> Arrays.asList(split).contains(s.getTypeNo()))
                .collect(Collectors.toList())
                .stream()
                .sorted(Comparator.comparing(ListType::getTypeNo))
                .map(ListType::getTypeName)
                .collect(Collectors.joining("/")));
    }

    /**
     * 处理整改复查的逻辑
     */
    private void handleRectifyReviews(BizProjectCheckRecord bizProjectCheckRecord, List<SysUser> sysUsers) {
        //根据记录id查询出整改复查记录
        List<BizRectifyReview> bizRectifyReviews = bizRectifyReviewMapper.selectListByOperatorType(bizProjectCheckRecord.getId(), null);
        bizProjectCheckRecord.setRectifyReviews(bizRectifyReviews);
        bizProjectCheckRecord.setReviewOperatorTime(bizRectifyReviews.stream()
                .filter(s -> "1".equals(s.getOperatorType()))
                .reduce((a, b) -> b)
                .orElse(new BizRectifyReview()).getOperatorTime());

        bizProjectCheckRecord.setRectifyOperatorTime(bizRectifyReviews.stream()
                .filter(s -> "2".equals(s.getOperatorType()))
                .reduce((a, b) -> b)
                .orElse(new BizRectifyReview()).getOperatorTime());

        bizProjectCheckRecord.getRectifyReviews().forEach(bizRectifyReview -> {

            bizRectifyReview.setOperatorerPhone(sysUsers.stream()
                    .filter(s -> s.getUserName().equals(bizRectifyReview.getOperatorer()))
                    .findFirst()
                    .orElse(new SysUser()).getPhonenumber());
            //获取复查图片
            List<BizSafeAttach> bizSafeAttaches = bizSafeAttachMapper.selectBizSafeAttachList(BizSafeAttach.builder().bizId(bizRectifyReview.getId()).build());
//            bizRectifyReview.setAttachList(bizSafeAttaches);
        });
        NameUtils.handleUserNameToNickName(bizRectifyReviews, sysUsers, "operatorer", "removeUserName");
    }

    /**
     * 处理延期申请的逻辑
     */
    private void handleExtensionRequests(BizProjectCheckRecord bizProjectCheckRecord) {

        //根据记录id查询出延期申请记录
        List<BizExtensionRequests> bizExtensionRequests = bizExtensionRequestsMapper.selectAllByRecordId(bizProjectCheckRecord.getId());
        BizExtensionRequests bizExtensionRequests1 = bizExtensionRequests.stream()
                .filter(s -> "0".equals(s.getStatus()))
                .findFirst()
                .orElse(null);
        //判断是否有延期申请记录如果在申请中则状态为0
        bizProjectCheckRecord.setExtensionStatus(
                "1".equals(bizProjectCheckRecord.getOverdueRectification())
                        ? "0"
                        : bizExtensionRequests1 != null
                        ? "1"
                        : "2"
        );
        if ("1".equals(bizProjectCheckRecord.getExtensionStatus()) && bizExtensionRequests1 != null) {
            bizProjectCheckRecord.setExtensionTime(bizExtensionRequests1.getExtensionTime());
        }

        bizProjectCheckRecord.setBizExtensionRequests(bizExtensionRequests);
    }

    /**
     * 处理责任区域的逻辑
     */
    private void handleDutyArea(List<BizDutyRegion> regionList, BizProjectCheckRecord bizProjectCheckRecord) {
        //获取责任区域
        BizDutyRegion bizDutyRegion = regionList.stream().filter(s -> s.getId().toString().equals(bizProjectCheckRecord.getDutyArea())).findFirst().orElse(new BizDutyRegion());
        //获取责任区域的父级
        Set<Long> parentsRegion = bizExcludePlanTaskService.getParentsRegion(regionList, bizDutyRegion);
        //获取责任区域的父级名称
        bizProjectCheckRecord.setDutyArea(getRegionNames(regionList, parentsRegion));
    }

    /**
     * 处理分包单位和罚款逻辑
     */
    private void handleSubpackageUnit(List<SysUser> sysUsers, BizProjectCheckRecord bizProjectCheckRecord, List<CommonCooperator> commonCooperators) {

        //查询检查记录的分包单位
        Set<String> subpackageUnit = Arrays.stream(bizProjectCheckRecord.getSubpackageUnit().split(","))
                .filter(StrUtil::isNotEmpty)
                .collect(Collectors.toSet());

        //查看是否有罚款记录
        List<BizSafeFine> bizSafeFines = bizSafeFineMapper.selectListByRecordId(bizProjectCheckRecord.getId());

        List<BizSafeFineVO> bizSafeFineVOList = new ArrayList<>();
        //遍历分包单位,插入罚款记录
        subpackageUnit.forEach(item -> {
            BizSafeFineVO bizSafeFineVO = new BizSafeFineVO();
            List<BizSafeFine> bizSafeFineList = bizSafeFines.stream().filter(s -> s.getFineUser().equals(item)).collect(Collectors.toList());

            NameUtils.handleUserNameToNickName(bizSafeFineList, sysUsers, "createBy");

            bizSafeFineVO.setBizSafeFines(bizSafeFineList);
            bizSafeFineVO.setFineCount(bizSafeFineList.isEmpty() ? null : bizSafeFineList.size());
            bizSafeFineVO.setSubpackageUnit(commonCooperators.stream()
                    .filter(s -> s.getId().toString().equals(item))
                    .findFirst()
                    .orElse(new CommonCooperator()).getEnterpriseName());
            bizSafeFineVOList.add(bizSafeFineVO);
        });
        bizProjectCheckRecord.setBizSafeFineVOList(bizSafeFineVOList);

        //判断分包单位不等于null的时候分割分包单位过滤查询用","拼接名字
        if (StringUtils.isNotEmpty(bizProjectCheckRecord.getSubpackageUnit())) {
            bizProjectCheckRecord.setSubpackageUnit(commonCooperators.stream()
                    .filter(s -> Arrays.asList(bizProjectCheckRecord.getSubpackageUnit().split(",")).contains(s.getId().toString()))
                    .map(CommonCooperator::getEnterpriseName)
                    .collect(Collectors.joining(",")));
        }
    }
}
